#include "sysmanage.h"
#include "adc_dev.h"
#include "time.h"
#include "comm.h"

devInfodef devInfoDat = {0};

/**
  * @brief  EEPROM 驱动打开函数
  * @param  None
  * @retval 驱动对象
  */
rt_device_t eeprom_device_open(void)
{
	rt_device_t device;

	device = rt_device_find("EEPROM");
  if (device != RT_NULL)
  {
  	if(!(device->open_flag & RT_DEVICE_OFLAG_OPEN))
  	{
      rt_device_open(device,RT_DEVICE_OFLAG_RDWR);
  	}
  }
	else
	{
		RT_ASSERT(RT_NULL);
	}

  return device;
}

/**
  * @brief  EEPROM 驱动关闭
  * @param  EEPROM 驱动对象
  * @retval None
  */
void eeprom_device_close(rt_device_t dev)
{
	if(dev != RT_NULL)
	{
		rt_device_close(dev);
	}
}

/* --------------------------------------------------------------------------*/
/**
 * @Synopsis  EEPROM 保存图片数据
 *
 * @Param data  图片地址
 */
/* ----------------------------------------------------------------------------*/
void eeprom_save_data(rt_uint8_t *data)
{
	rt_device_t dev;

	dev = eeprom_device_open();
	//rt_device_write(dev, EEPROM_PIC0_ADDR, data, ELS_PIC_BUF_SIZE);
	eeprom_device_close(dev);
}


/* --------------------------------------------------------------------------*/
/**
 * @Synopsis  EEPROM 加载图片数据
 *
 * @Param data 图片数据缓存
 */
/* ----------------------------------------------------------------------------*/
void eeprom_load_data(rt_uint8_t *data)
{
	rt_device_t dev;

	dev = eeprom_device_open();
	//rt_device_read(dev, EEPROM_PIC0_ADDR, data, ELS_PIC_BUF_SIZE);
	eeprom_device_close(dev);
}



void get_dev_mac(rt_uint8_t *id)
{
	rt_uint32_t CpuID[3];
	
	//获取CPU唯一ID
	CpuID[0]=*(int *)(0x1ffff7e8);
	CpuID[1]=*(int*)(0x1ffff7ec);
	CpuID[2]=*(int*)(0x1ffff7f0);
	//转换为字节
	uint32_to_str(CpuID[0], id);
	uint32_to_str(CpuID[1], &id[4]);
	uint32_to_str(CpuID[2], &id[8]);
}


/* --------------------------------------------------------------------------*/
/**
 * @Synopsis  加载EEPROM到设备信息结构体
 *
 * @Returns   
 */
/* ----------------------------------------------------------------------------*/
rt_err_t devinfo_loader(void)
{
	rt_device_t dev;
	rt_size_t size;
	
	dev = eeprom_device_open();
	size = rt_device_read(dev, EEPROM_DEVINFO_ADDR, &devInfoDat, sizeof(devInfodef));
	eeprom_device_close(dev);
	if(size != sizeof(devInfodef))
	{
		return RT_ERROR;	
	}
	return RT_EOK;
}


/* --------------------------------------------------------------------------*/
/**
 * @Synopsis  保存设备信息到EEPROM
 *
 * @Returns   
 */
/* ----------------------------------------------------------------------------*/
rt_err_t devinfo_save(void)
{
	rt_device_t dev;
	rt_size_t size;

	dev = eeprom_device_open();
	size = rt_device_write(dev, EEPROM_DEVINFO_ADDR, &devInfoDat, sizeof(devInfodef));
	eeprom_device_close(dev);
	if(size != sizeof(devInfodef))
	{
		return RT_ERROR;	
	}
	return RT_EOK;
}


/* --------------------------------------------------------------------------*/
/**
 * @Synopsis  获取设备信息结构体
 *
 * @Param data
 */
/* ----------------------------------------------------------------------------*/
devInfodef* devinfo_get(void)
{
	return &devInfoDat;
}


/* --------------------------------------------------------------------------*/
/**
 * @Synopsis  设置设备信息结构体
 *
 * @Param data
 */
/* ----------------------------------------------------------------------------*/
void devinfo_set(devInfodef *data)
{
	devInfoDat = (*data);
}



void update_rtc_value(rt_uint32_t date)
{
	rt_device_t dev;

	dev = rt_device_find("rtc");
	if(dev != RT_NULL)
	{
		rt_device_init(dev);
		rt_device_open(dev,RT_DEVICE_FLAG_RDWR);
		rt_device_control(dev, RT_DEVICE_CTRL_RTC_SET_TIME, &date);
		rt_device_close(dev);
	}
	else
	{
		rt_kprintf("None Find RTC Device\n");
	}
}


void devinfo_default(rt_uint8_t *ip,rt_uint16_t port,rt_uint8_t *id)
{
	rt_strncpy(devInfoDat.url , ip, rt_strlen(ip));
	devInfoDat.port = port;
	rt_strncpy(devInfoDat.url, id, rt_strlen(id));
}

